package com.dqyt.app.mongo;
import com.dqyt.app.bean.Sample;
import com.dqyt.app.bean.SampleLhq;
import com.mongodb.client.result.DeleteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SampleLhqDao {

    /**
     * 注入MongoTemplate
     */
    @Autowired
    protected MongoTemplate mongoTemplate;


    /**
     * 新建数据到数据库
     *
     * @return
     */
    public void save(SampleLhq sampleLhq) {
        mongoTemplate.save(sampleLhq); // 保存数据到数据库
    }



    /**
     * 查询某条数据的详细信息
     * @param ccbh  出厂编号
     * @param jlbh  记录编号
     * @param erweima
     * @return
     */
    public List<SampleLhq> getSample( String ccbh, String jlbh, String erweima) {
        // 构建查询条件
        Query query = new Query();
        if (null != ccbh && !ccbh.equals("")) {
            query.addCriteria(new Criteria("ccbh").is(ccbh));
        }
        if (null != jlbh && !jlbh.equals("")) {
            query.addCriteria(new Criteria("jlbh").is(jlbh));
        }
        if (null != erweima && !erweima.equals("")) {
            query.addCriteria(new Criteria("erweima").is(erweima));
        }
        query.limit(10).with(Sort.by(Sort.Direction.DESC, "addTime"));
        List<SampleLhq> list = mongoTemplate.find(query, SampleLhq.class, SampleLhq.TableName); // 取得满足条件的数据
        return list;
    }



    /**
     * 更具记录病号查询样品信息
     * @param jlbh
     * @return
     */
    public SampleLhq getPcSampleByJlbh(String jlbh){
        // 构建查询条件
        Query query = new Query();
        query.addCriteria(new Criteria("jlbh").is(jlbh));
        query.limit(10).with(Sort.by(Sort.Direction.DESC, "addTime"));
        List<SampleLhq> list = mongoTemplate.find(query, SampleLhq.class, SampleLhq.TableName); // 取得满足条件的数据
        if(list.size()>0){
            return  list.get(0);
        }else{
            return null;
        }
    }


    /**
     * 询某条数据的详细信息
     * @param bh
     * @return
     */
    public SampleLhq getSampleByBH(String bh) {
        // 构建查询条件
        Query query = new Query(new Criteria("bh").is(bh));
        SampleLhq ret = mongoTemplate.findOne(query, SampleLhq.class);
        return ret;
    }


    /**
     *
     * @param jlbh  记录编号
     * @param ccbh  标识编号
     * @param jlrId  记录人id
     * @param erweima  二维码
     * @return
     */
    public List<SampleLhq>  getSampleListPage(int pageNum, int pageSize, String jlbh,String ccbh,long jlrId,String erweima){
        // 构建查询条件
        Query query = new Query();
        if (null != jlbh && !jlbh.equals("")) {
            query.addCriteria(new Criteria("jlbh").is(jlbh));
        }

        if (null != ccbh && !ccbh.equals("")) {
            query.addCriteria(new Criteria("ccbh").is(ccbh));
        }

        if (0 != jlrId) {
            query.addCriteria(new Criteria("jlrId").is(jlrId));
        }
        if (null != erweima && !erweima.equals("")) {
            query.addCriteria(new Criteria("erweima").is(erweima));
        }
        query.limit(pageSize).skip(pageNum);
        List<SampleLhq> list = mongoTemplate.find(query, SampleLhq.class, SampleLhq.TableName); // 取得满足条件的数据
        return list;
    }

    /**
     *
     * @param jlbh
     * @param ccbh
     * @param jlrId
     * @param erweima
     * @return
     */
    public long getSampleListTotle(String jlbh,String ccbh,long jlrId,String erweima){
        // 构建查询条件
        Query query = new Query();
        if (null != jlbh && !jlbh.equals("")) {
            query.addCriteria(new Criteria("jlbh").is(jlbh));
        }
        if (null != ccbh && !ccbh.equals("")) {
            query.addCriteria(new Criteria("ccbh").is(ccbh));
        }
        if (0 != jlrId) {
            query.addCriteria(new Criteria("jlrId").is(jlrId));
        }
        if (null != erweima && !erweima.equals("")) {
            query.addCriteria(new Criteria("erweima").is(erweima));
        }
        List<SampleLhq> list = mongoTemplate.find(query, SampleLhq.class, SampleLhq.TableName); // 取得满足条件的数据
        return list.size();
    }



    /**
     * 新建数据到数据库
     *
     * @return
     */
    public long deleteById(String bh) {
        Query query = Query.query(Criteria.where("bh").is(bh));
        DeleteResult deleteResult = mongoTemplate.remove(query, SampleLhq.class);
        long res = deleteResult.getDeletedCount();
        return res;
    }

}
